package com.neo.task;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * Created by fangzhipeng on 2017/4/19.
 *  @Scheduled(fixedRate = 5000) ：上一次开始执行时间点之后5秒再执行
 * @Scheduled(fixedDelay = 5000) ：上一次执行完毕时间点之后5秒再执行
 * @Scheduled(initialDelay=1000, fixedRate=5000) ：第一次延迟1秒后执行，之后按fixedRate的规则每5秒执行一次
 * @Scheduled(cron="  /5   ") ：通过cron表达式定义规则
 */

@Component
public class Scheduler2Task {

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    private int count=0;

    @Autowired
    private  CreateHttp createHttp;

    //@Scheduled(fixedRate = 900000)
    @Scheduled(fixedRate = 6000)
    public void reportCurrentTime() {
        try {
            Integer[] ids ={185,190,192,183,195,196,197,199,200};
            Random random = new Random();
            int num = random.nextInt(ids.length);
            Integer id = ids[num];
            System.out.println("开始执行刷票： " + id + " : "+dateFormat.format(new Date()) +"次数:"+(count++));
            //http://7zhongzui.com//Home/index.php?m=Index&a=vote&vid=1955&id=199&tp=HTTP/1.1
           // String GET_URL = "http://7zhongzui.com//Home/index.php?m=Index&a=vote&vid=2167&id="+id+"&tp=HTTP/1.1";
           // String GET_URL =  "http://7zhongzui.com//Home/index.php?m=Index&a=vote&vid=1955&id="+id+"&tp=HTTP/1.1";
            //String GET_URL = "http://7zhongzui.com//Home/index.php?m=Index&a=vote&vid=1898&id="+id+"&tp=HTTP/1.1"; //同学
            String GET_URL = "http://7zhongzui.com//Home/index.php?m=Index&a=vote&vid=2035&id="+id+"&tp=HTTP/1.1"; //同学

            createHttp.httpURLConectionGET(GET_URL);
            System.out.println("刷票结束：" + dateFormat.format(new Date()));
        } catch (Exception e) {

        }


    }


    //@Scheduled(fixedRate = 10000)
    public void httpURLConnectionPOST() {

        try {

            System.out.println("开始执行刷票： "+dateFormat.format(new Date()) +"次数:"+(count++));

            createHttp.httpURLConnectionPOST();

            System.out.println("刷票结束：" + dateFormat.format(new Date()));
        } catch (Exception e) {
            System.out.println("失败");
        }
    }

}
